package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.entity.ReservationTry;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.mapper.ReservationTryMapper;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.ReservationTryService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
@Setter
public class ReservationTryServiceImpl extends ServiceImpl<ReservationTryMapper, ReservationTry> implements ReservationTryService {

    @Autowired
    private ReservationTryMapper reservationTryMapper; //预约试听映射

//    /**
//     * @param reservationTry 预约试听
//     * @return ResultInfo
//     * @Author wuweinan
//     * @Description 插入预约试听时间设置
//     * @Date 2021-6-29 16:34
//     */
//    @Override
//    public ResultInfo insertTryReservationTime(ReservationTry reservationTry) {
//        log.info("ReservationTryServiceImpl的insertTryReservationTime进入");
//        reservationTry.setCt(LocalDateTime.now());
//        //添加一条预试信息
//        Integer insertCount = reservationTryMapper.insert(reservationTry);
//
//
//        //检验插入的条数是否正确
//        if (insertCount != 1) {
//            throw new SystemException(ReturnCode.COMMON_RESERVATION_TRY_WRONG_INSERT_COUNT);
//        }
//
//        log.info("ReservationTryServiceImpl的insertTryReservationTime返回");
//        return ResultInfo.success();
//    }


    /**
     * @param reservationTry 预约试听
     * @return ResultInfo
     * @Author wuweinan
     * @Description 更新预约试听时间设置
     * @Date 2021-6-29 16:34
     */
    @Override
    public ResultInfo updateTryReservationTime(ReservationTry reservationTry) {
        //根据学生id修改预约回访的datetime字段,因为student表和reservationTry表示一对一关系
        UpdateWrapper<ReservationTry> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("datetime", reservationTry.getDatetime()).set("uu", reservationTry.getUu()).
                set("autor", reservationTry.getAutor()).
                eq("id", reservationTry.getId())
                .eq("student_id", reservationTry.getStudentId());

        //更新条数检验
        int update = reservationTryMapper.update(null, updateWrapper);
        if (update != 1) {
            throw new SystemException(ReturnCode.UPDATE_RESERVATION_TRY_FAIL);
        }
        return ResultInfo.success();
    }
}
